<?php
define('QTRANS_COOKIE_EXPIRES', (30 * 24 * 60 * 60));
define('QTRANS_COOKIE_NAME', str_replace('.', '_', DOMAIN_CURRENT_SITE) . '_qtrans_val');
define('QTRANS_REDIRECT_SCHEME', 'http');


$redir = array(
     'ru' => '',
     'uk' => 'uk'
);

$current_lang = qtrans_getLanguage();
$ref = $_SERVER['HTTP_REFERER'];
$ref_parse = parse_url($ref);

if (array_key_exists(QTRANS_COOKIE_NAME, $_COOKIE)) {
    $client_cookie = $_COOKIE[QTRANS_COOKIE_NAME];

    if (!array_key_exists('host', $ref_parse) || $ref_parse['host'] != DOMAIN_CURRENT_SITE) {
        if ($client_cookie != $current_lang) {
            $location = QTRANS_REDIRECT_SCHEME . '://' . DOMAIN_CURRENT_SITE . '/' . (array_key_exists($client_cookie, $redir) ? $redir[$client_cookie] : '') . (array_key_exists('REQUEST_URI', $_SERVER) && $_SERVER['REQUEST_URI'] != '/' ? $_SERVER['REQUEST_URI'] : '');
            wp_redirect($location);
            exit;
        }
    }
}

if (array_key_exists('SERVER_NAME', $_SERVER) && $_SERVER['SERVER_NAME'] == DOMAIN_CURRENT_SITE) {
    qtrans_set_lang($current_lang);
}

function qtrans_set_lang($lang) {
    setcookie(QTRANS_COOKIE_NAME, $lang, time()+QTRANS_COOKIE_EXPIRES, '/', '.lanet.ua');
}

